package school.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import school.domain.Goods;
import school.domain.Orders;
import school.domain.User;
import school.mapper.GoodsMapper;
import school.mapper.OrdersMapper;
import school.mapper.UserMapper;
import school.support.BaseController;
import school.support.pojo.OrdersPojo;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@RequestMapping("member")
@Controller
public class MemberController extends BaseController {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 支付
     *
     * @param orders
     * @return
     */
    @RequestMapping("pay")
    public String pay(Orders orders) {
        if (null == sessionUser()) {
            session.setAttribute("errorMsg", "请先登录");
            return "redirect:/login";
        }
        Goods goods = goodsMapper.selectByPrimaryKey(orders.getGoodsid());
        orders.setPrice(goods.getPrice() * orders.getCount());
        orders.setAddtime(new Date());
        orders.setStatus("未支付");
        orders.setUserid(sessionUser().getId());
        ordersMapper.insertSelective(orders);
        return "redirect:order";
    }

    @RequestMapping("cancelOrder/{id}")
    public String cancelOrder(@PathVariable Integer id) {
        if (null == sessionUser()) {
            session.setAttribute("errorMsg", "请先登录");
            return "redirect:/index";
        }
        Orders orders = ordersMapper.selectByPrimaryKey(id);
        orders.setStatus("已取消");
        ordersMapper.updateByPrimaryKey(orders);
        return refresh();
    }


    @RequestMapping("delOrder/{id}")
    public String delOrder(@PathVariable Integer id) {
        if (null == sessionUser()) {
            session.setAttribute("errorMsg", "请先登录");
            return "redirect:/index";
        }
        ordersMapper.deleteByPrimaryKey(id);
        return refresh();
    }

    @RequestMapping("order")
    public String order(Model model) {
        if (null == sessionUser()) {
            session.setAttribute("errorMsg", "请先登录");
            return "redirect:/login";
        }
        Example example = new Example(Orders.class);
        example.createCriteria().andCondition("o.userid = ", sessionUser().getId());
        List<OrdersPojo> pojoList = ordersMapper.selectPoByExample(example);
        model.addAttribute("orderList", pojoList);
        return "order";
    }

    @RequestMapping("index")
    public String index(Model model) {
        model.addAttribute("errorMsg", session.getAttribute("errorMsg"));
        session.removeAttribute("errorMsg");
        if (frontUser() == null) return "redirect:/login";
        User user = userMapper.selectByPrimaryKey(frontUser().getId());
        model.addAttribute(user);
        return "member";
    }

    @RequestMapping("edit")
    public String edit(Model model) {
        model.addAttribute("errorMsg", session.getAttribute("errorMsg"));
        session.removeAttribute("errorMsg");
        if (frontUser() == null) return "redirect:/login";
        User user = userMapper.selectByPrimaryKey(frontUser().getId());
        model.addAttribute(user);
        return "edit";
    }


    /**
     * 更新个人信息
     *
     * @param user
     * @return
     */
    @RequestMapping("update")
    public String update(User user, Model model) {
        if (user.getPassword() == null || user.getPassword().isEmpty()) user.setPassword(null);
        user.setId(frontUser().getId());
        userMapper.updateByPrimaryKeySelective(user);
        session.setAttribute("errorMsg", "修改成功");
        session.setAttribute("sessionUser", user);
        return "redirect:index";
    }

    /**
     * 修改密码
     *
     * @return
     */
    @RequestMapping("modfy")
    public String modfy(Model model, String password, String newpassword, String repassword) {
        if (!newpassword.equals(repassword)) {
            session.setAttribute("errorMsg", "两次密码不一样");
            return "redirect:index";
        }
        User user = userMapper.selectByPrimaryKey(frontUser().getId());
        if (!user.getPassword().equals(password)) {
            session.setAttribute("errorMsg", "原密码错误");
            return "redirect:index";
        }
        user.setPassword(newpassword);
        userMapper.updateByPrimaryKeySelective(user);
        session.setAttribute("sessionUser", user);
        session.setAttribute("errorMsg", "修改成功");
        return "redirect:index";
    }

}
